/***********************************************************************
    filename:   DaterTime.cpp
    created:    10/4/2012
    author:     4Ys

    purpose:    Implements a base class for time used in Dater
*************************************************************************/

#include "DaterTime.h"

namespace Dater
{
	//----------------------------------------------------------------------------//
	time_t currentTime = time(0); 
	tm* Time::_todayTime = localtime(&currentTime);

	//----------------------------------------------------------------------------//
	Time::Time():_rawTime()
	{
		_commonTime = new tm;
		setTime();
	}

	//----------------------------------------------------------------------------//
	Time::Time(int* timeData):_rawTime()
	{
		_commonTime = new tm;
		setTime(timeData);
	}

	//----------------------------------------------------------------------------//
	Time::~Time()
	{
		delete _commonTime;
	}

	//----------------------------------------------------------------------------//
	Time::Time(tm* timeData)
	{
		_commonTime = new tm;
		setTime(timeData);
	}

	Time::Time(std::string day, std::string time)
	{
		_commonTime = new tm;

		size_t pos1 = day.find("-");
		size_t pos2 = day.find("-", pos1 + 1);

		_commonTime->tm_sec = 0;
		_commonTime->tm_min = 0;
		_commonTime->tm_hour = 0;
		_commonTime->tm_mday = atoi(day.substr(pos2 + 1).c_str());
		_commonTime->tm_mon = atoi(day.substr(pos1 + 1, pos2 - pos1 - 1).c_str()) - 1;
		_commonTime->tm_year = atoi(day.substr(0,pos1).c_str()) - 1900;

		if(time.size() != 0)
		{
			pos1 = time.find(":");

			_commonTime->tm_min = atoi(time.substr(pos1 + 1).c_str());
			_commonTime->tm_hour = atoi(time.substr(0, pos1).c_str());
		}

		_rawTime = mktime(_commonTime);
	}

	//----------------------------------------------------------------------------//
	Time::Time(const Time& time)
	{
		_commonTime = new tm;
		_commonTime->tm_hour = time._commonTime->tm_hour;
		_commonTime->tm_isdst = time._commonTime->tm_isdst;
		_commonTime->tm_mday = time._commonTime->tm_mday;
		_commonTime->tm_min = time._commonTime->tm_min;
		_commonTime->tm_mon = time._commonTime->tm_mon;
		_commonTime->tm_sec = time._commonTime->tm_sec;
		_commonTime->tm_wday = time._commonTime->tm_wday;
		_commonTime->tm_yday = time._commonTime->tm_yday;
		_commonTime->tm_year = time._commonTime->tm_year;

		_rawTime = mktime(_commonTime);
	}

	//----------------------------------------------------------------------------//
	Time& Time::operator=(const Time& time)
	{
		if(&time != this)
		{
			delete _commonTime;
			_commonTime = new tm;
			_commonTime->tm_hour = time._commonTime->tm_hour;
			_commonTime->tm_isdst = time._commonTime->tm_isdst;
			_commonTime->tm_mday = time._commonTime->tm_mday;
			_commonTime->tm_min = time._commonTime->tm_min;
			_commonTime->tm_mon = time._commonTime->tm_mon;
			_commonTime->tm_sec = time._commonTime->tm_sec;
			_commonTime->tm_wday = time._commonTime->tm_wday;
			_commonTime->tm_yday = time._commonTime->tm_yday;
			_commonTime->tm_year = time._commonTime->tm_year;

			_rawTime = mktime(_commonTime);
		}

		return *this;
	}

	//----------------------------------------------------------------------------//
	void Time::setTime()
	{
		_rawTime = time(&_rawTime);
		localtime_s(_commonTime, &_rawTime);
	}

	//----------------------------------------------------------------------------//
	void Time::setTime(int* timeData)
	{
		_commonTime->tm_sec = 0;
		_commonTime->tm_min = timeData[4];
		_commonTime->tm_hour = timeData[3];
		_commonTime->tm_mday = timeData[2];
		_commonTime->tm_mon = timeData[1] - 1;
		_commonTime->tm_year = timeData[0] - 1900;
		_rawTime = mktime(_commonTime);
	}

	//----------------------------------------------------------------------------//
	void Time::setTime(tm* timeData)
	{
		_commonTime->tm_hour = timeData->tm_hour;
		_commonTime->tm_isdst = timeData->tm_isdst;
		_commonTime->tm_mday = timeData->tm_mday;
		_commonTime->tm_min = timeData->tm_min;
		_commonTime->tm_mon = timeData->tm_mon;
		_commonTime->tm_sec = timeData->tm_sec;
		_commonTime->tm_wday = timeData->tm_wday;
		_commonTime->tm_yday = timeData->tm_yday;
		_commonTime->tm_year = timeData->tm_year;
		_rawTime = mktime(_commonTime);
	}

	//----------------------------------------------------------------------------//
	tm* Time::getTime()
	{
		return _commonTime;
	}

	//----------------------------------------------------------------------------//
	time_t Time::getRawTime()
	{
		return _rawTime;
	}

	//----------------------------------------------------------------------------//
	int Time::getTimeDiffer(const Time& timeData2) const
	{
		return int(_rawTime - timeData2._rawTime);
	}

	//----------------------------------------------------------------------------//
}


